const format = (date, fmt) => {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + ''
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str))
        }
    }
    return fmt
}

function padLeftZero(str) {
    return ('00' + str).substr(str.length)
}
//格式化日期
export const formatDate = time => {
    if (time == '' || time == null || time == undefined) return '';
    // if (typeof time === 'string') time = parseInt(time*1000)
    let ua = navigator.userAgent.toLowerCase();
    let date
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        //ios
        date = new Date(time.replace(/-/g, "/"));

    } else if (/(Android)/i.test(navigator.userAgent)) {
        date = new Date(time);
    }
    return format(date, 'yyyy/MM/dd hh:mm')
}

export const formatDate1 = time => {
    if (time == '' || time == null || time == undefined) return '';
    // if (typeof time === 'string') time = parseInt(time*1000)
    let ua = navigator.userAgent.toLowerCase();
    let date
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        //ios
        date = new Date(time.replace(/-/g, "/"));

    } else if (/(Android)/i.test(navigator.userAgent)) {
        date = new Date(time);
    }
    return format(date, 'yyyy.MM.dd')
}
export const imageUrl = url => {
    let str = RegExp('http');
    let imgUrl = "https://xueyizhi.oss-cn-shenzhen.aliyuncs.com/"
    let newUrl;
    //通过三元运算符进行判断该图片是否含有http域名，没有就拼接上去
    str.test(url) ? newUrl = url : newUrl = imgUrl + url;
    return newUrl
}
export const formatDate2 = time => {
    if (time == '' || time == null || time == undefined) return '';
    return format(time, 'yyyy-MM')
}

export const formatDate3 = time => {
    if (time == '' || time == null || time == undefined) return '';
    return format(time, 'yyyy年MM月')
}

//格式化金额
export const price = value => {
    if (!value) return '0.00';
    const _value = parseFloat(value);
    if (isNaN(_value)) return value;
    return _value.toFixed(2);
}

//格式化时间日期取值(日期 无时分秒)
export const formatTime = time => {
    if (time == '' || time == null || time == undefined) return '';
    if (typeof time === 'string') time = parseInt(time)
    let date = new Date(time);
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    return year + '-' + month + '-' + day;
}